<!--
 * @Author: your name
 * @Date: 2020-12-28 11:32:50
 * @LastEditTime: 2020-12-29 11:56:29
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \H5 2010 studyd:\H5 2010 练习\第三阶段 Vue + node\DAY 7\panda\src\views\home\index.vue
-->
<template>
  <div class="home">
    <!-- 主页
    <br />
    <br />
    <router-link to="/">首页</router-link> |
    <router-link to="/mine">个人中心</router-link> |
    <router-link to="/detail">详情</router-link> |
    <router-link to="/cart">购物车</router-link> |
    <router-link to="/category">分类</router-link> -->

    <div class="swiper-container">
      <div class="swiper-wrapper">
          <div
            class="swiper-slide"
            v-for="banner in banners"
            :key="banner.id"
          >
            <img :src="banner.imageUrl" alt="banner" /></div>
      </div>
      <!-- 如果需要分页器 -->
      <div class="swiper-pagination"></div>

      <!-- 如果需要导航按钮 -->
      <div class="swiper-button-prev"></div>
      <div class="swiper-button-next"></div>
    </div>
  </div>
</template>

<script>
import { getHomePageData } from '@/api/home'
import Swiper from 'swiper/swiper-bundle.js'
import 'swiper/swiper-bundle.css'

export default {
  name: 'Home',
  data() {
    return {
      banners: []
    }
  },
  created() {
    getHomePageData() // 当时返回的是一个 promise 对象，可以使用then
      .then(res => {
        console.log('首页数据:', res)

        // 更新 banners 数据 (轮播图数据)
        this.banners = res.banners

        // 在下一次 DOM 更新循环结束后，创建 Swiper 实例对象
        this.$nextTick(() => {
          /* eslint-disable-next-line */
          new Swiper('.swiper-container', {
            loop: true, // 循环模式选项

            // 如果需要分页器
            pagination: {
              el: '.swiper-pagination'
            },

            // 如果需要前进后退按钮
            navigation: {
              nextEl: '.swiper-button-next',
              prevEl: '.swiper-button-prev'
            }
          })
        })
      })
    //   request.get('/tabs?sa=')
    //     .then(res => console.log('api:', res))
  }

}
</script>

<style lang="scss" scoped>

</style>
